﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace LopDaGiac
{
    public abstract class DaGiac
    {
        // bien thanh vien
        private int socanh;
        protected double[] cdCanh;
		int k = 0;
        // ham khoi tao
        public DaGiac(int socanh)
        {
            this.socanh = socanh;
            cdCanh = new double[socanh];
        }
        // ham thanh vien
        public virtual void InThongTin()
        {
            Console.WriteLine("So canh: {0}",socanh);
        }
        // phuong thuc thuan ao
        public abstract void TinhChuVi();
        public abstract void TinhDienTich();
    }
    // Lop ngoai le
    public class NhapSoDuong : ApplicationException
    {
        public NhapSoDuong(string thongbao)
            :base(thongbao)
        {
        }
    }
    // Lop co so (base class) : DaGiac
    // Lop dan xuat (derived class): LopHinhVuong
    public class LopHinhVuong : DaGiac
    {
        // ham tao
        public LopHinhVuong()
            : base(1)
        {
            NhapLaiCanh:
            Console.Write("Nhap canh hinh vuong: ");
            try
            {
                cdCanh[0] = Double.Parse(Console.ReadLine());
                if (cdCanh[0] <= 0)
                    throw new NhapSoDuong("Phai nhap so >0!!");
            }
            catch (FormatException)
            {
                Console.WriteLine("Nhap chuoi so!!");
                goto NhapLaiCanh;
            }
            catch (NhapSoDuong ex)
            {
                Console.WriteLine("{0}",ex.Message);
                goto NhapLaiCanh;
            }
                    
        }
        // override
        public override void InThongTin()
        {
            base.InThongTin();
            Console.WriteLine("Chieu dai canh hinh vuong: {0:0.00}",
                cdCanh[0]);
        }
        // implement abstract methods   
        public override void TinhChuVi()
        {
            Console.WriteLine("Chu vi: {0:0.00}",
                cdCanh[0]*4);
        }
        public override void TinhDienTich()
        {
            Console.WriteLine("Dien tich: {0:0.00}",
                cdCanh[0] * cdCanh[0]);
        }
    }
    //
    class Program
    {
        static void Main(string[] args)
        {
           // DaGiac gd1 = new DaGiac();
            LopHinhVuong hv1 = new LopHinhVuong();
            hv1.InThongTin();
            hv1.TinhChuVi();
            hv1.TinhDienTich();
        }
    }
}
